<template>
  <view class="container">
    <view class="form">
      <text class="title">注册</text>
      <input class="input" placeholder="请输入用户名" v-model="username" />
      <input class="input" placeholder="请输入密码" type="password" v-model="password" />
      <input class="input" placeholder="请确认密码" type="password" v-model="confirmPassword" />
      <button class="btn" type="primary" @click="handleRegister">注册</button>
      <view class="link">
        <text>已有账号？</text>
        <!-- 跳转到登录页（保持原有导航） -->
        <navigator url="/pages/Login/Login">去登录</navigator>
      </view>
    </view>
  </view>
</template>
<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: ''
    };
  },
  methods: {
    handleRegister() {
      if (!this.username || !this.password || !this.confirmPassword) {
        uni.showToast({
          title: '请输入完整信息',
          icon: 'none'
        });
        return;
      }
      if (this.password !== this.confirmPassword) {
        uni.showToast({
          title: '两次输入密码不一致',
          icon: 'none'
        });
        return;
      }
      // 开启注册加载状态
      uni.showLoading({
        title: '注册中...'
      });
      console.log("点击注册，准备调用接口发送数据", {
        username: this.username,
        password: this.password
      });

      // 调用云函数提交注册数据（保持原有addUser云函数）
      uniCloud.callFunction({
        name: 'addUser',
        data: {
          name: this.username,
          password: this.password
        },
        success: (res) => {
          console.log("注册接口调用成功", res);
          uni.hideLoading();
          if (res.result?.success !== false) {
            uni.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 1500
            });
            // 注册成功跳登录页（保持原有逻辑）
            setTimeout(() => {
              uni.navigateTo({
                url: "/pages/Login/Login"
              });
            }, 1500);
          } else {
            uni.showToast({
              title: res.result?.errMsg || '注册失败',
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          console.error("注册接口调用失败", err);
          uni.hideLoading();
          uni.showToast({
            title: '注册失败：' + (err.errMsg || '网络异常'),
            icon: 'none',
            duration: 2000
          });
        }
      });
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f5f5f5;
}
.form {
  width: 80%;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.title {
  font-size: 20px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}
.input {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 10px;
  margin-bottom: 15px;
  width: 100%;
}
.btn {
  width: 100%;
  margin-bottom: 15px;
}
.link {
  text-align: center;
}
.link text, .link navigator {
  font-size: 14px;
}
.link navigator {
  color: #007aff;
}
</style>